package com.example.mall.mapper;

import com.example.mall.pojo.*;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

public interface UserMapper {

//    查询全部
    @Select("select mg_id as id,mg_name as username, mg_time as create_time ,mg_mobile as mobile,mg_email as email,r.role_name as role_name,mg_state from sp_manager m left join sp_role r on m.role_id = r.role_id ")
    List<UserOut> selectAll();

//    模糊查询
    @Select("select mg_id as id,mg_name as username, mg_time as create_time ,mg_mobile as mobile,mg_email as email,r.role_name as role_name,mg_state from sp_manager m left join sp_role r on m.role_id = r.role_id where m.mg_name like '%${query}%'")
    List<UserOut> selectAllByQuery(String mg_name);

//    删除新的用户
    @Delete("delete from sp_manager where mg_id = #{mg_id}")
    int deleteUser(Integer mg_id);

//    根据id查询用户列表
    @Select("select m.mg_id as id,r.role_id as rid,m.mg_name as username, m.mg_mobile as mobile,m.mg_email as email from sp_manager  m join  sp_role r  on m.role_id = r.role_id where m.mg_id = #{mg_id}")
    UserById selectById(Integer mg_id);

//    插入新的用户
    @Insert("insert into sp_manager(mg_name,mg_pwd,mg_email,mg_mobile,mg_time,role_id) values (#{username},#{password},#{email},#{mobile},1486720211,0)")
    int insertUser(UserOut userOut);

//    根据id修改用户状态
    @Update("update sp_manager set mg_state=#{state} where mg_id=#{id}")
    int updateStateById(@Param("state") Boolean state,@Param("id") Integer id);

//    根据id修改用户邮箱和电话
    @Update("update sp_manager set mg_email=#{email} ,mg_mobile=#{mobile} where mg_id=#{id}")
    int updateInfo(@Param("email") String email,@Param("mobile") String mobile,@Param("id") Integer id);

//    @Select("SELECT * FROM sp_roles where  role_id=#{rid}")
//    SpRole getRolesById(Integer id);

    @Update("update sp_manager set role_id=#{role_id} where  mg_id=#{mg_id}")
    int updateRoles(@Param("role_id") Integer rid, @Param("mg_id") Integer id);

//    分配角色
//    @Update("update sp_manager set role_id=#{rid} where mg_id=#{id}")
//    int updateRole(@Param("role_id") Integer rid,@Param("mg_id") Integer id);

//    根据id查询role名
//    @Select("select role_name from sp_role")
//    List<RoleList> selectRole();

}
